package com.project.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

@ManagedBean(name="mValidatorLib")
@SessionScoped
public class ValidatorLib {
	
	private Pattern pattern;
	private Matcher matcher;
	
	public ValidatorLib()
	{
		
		
	}
	
	public void EmailValidate(FacesContext context, UIComponent component,
			Object value)
	{
		  pattern = Pattern.compile("\\S+@\\S+\\.\\S+");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("E-mail validation failed.", 
							"Invalid E-mail format .");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
		  }
		  if(value.toString().length() > 255){
			  
				FacesMessage msg = 
					new FacesMessage("E-mail validation failed.", 
							"Invalid E-mail format (50 characters only)");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
		  }
		  
	}
	
	public void PasswordValidate(FacesContext context, UIComponent component,
			Object value)
	{ 
		  pattern = Pattern.compile("\\w{6,30}");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("Password validation failed.", 
							"Invalid Password format (Character Only and at least 6 character and max is 30).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
		  }
		  
	}
	
	public void DescriptionValidator(FacesContext context, UIComponent component,
			Object value)
	{
		pattern = Pattern.compile("^[\\S]\\s*\\S.*");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches())
		  {
			  
				FacesMessage msg = 
					new FacesMessage("Description validation failed.", 
							"Invalid Description Format (Please input something and white spaces are not allowed at beginning).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
		  }
		  if(value.toString().length() > 2000)
		  {
			  FacesMessage msg = 
					new FacesMessage("Description validation failed.", 
							"Invalid Description Format (2000 characters only).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
		  }
	}
	
	public void MessageValidator(FacesContext context, UIComponent component,
			Object value)
	{
		pattern = Pattern.compile("^[\\S]\\s*\\S.*");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("Message validation failed.", 
							"Invalid Message Format (Please input something).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}
	}
	
	public void FullnameValidator(FacesContext context, UIComponent component,
			Object value)
	{
		  pattern = Pattern.compile("^[a-zA-Z]\\s*\\S.*");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("Fullname validation failed.", 
							"Invalid Fullname Format (Please input something and no white spaces at the beginning).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
		 }
		  if(value.toString().length() > 50)
		  {
			  FacesMessage msg = 
					new FacesMessage("Fullname validation failed.", 
							"Invalid Fullname Format (50 Characters only).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
		  }
		  
	}
	
	public void SecurityAnswerValidator(FacesContext context, UIComponent component,
			Object value)
	{
		  pattern = Pattern.compile("^[\\S]\\s*\\S.*");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("Security Answer validation failed.", 
							"Invalid Security Answer format (Please input something).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}
		  
	}
	
	public void AddressValidator(FacesContext context, UIComponent component,
			Object value)
	{
		  pattern = Pattern.compile("^[\\S]\\s*\\S.*");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("Address validation failed.", 
							"Invalid Address format (Please input something and no white spaces at the beginning).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
		 }
		 if(value.toString().length() > 255)
		 {
			 FacesMessage msg = 
					new FacesMessage("Address validation failed.", 
							"Invalid Address format (Max 255 characters only).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
		 }
		  
	}
	
	public void PhoneValidator(FacesContext context, UIComponent component,
			Object value)
	{
		  pattern = Pattern.compile("[0-9]{4}-[0-9]{3}-[0-9]{3}");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("Phone validation failed.", 
							"Invalid Phone format (XXXX-XXX-XXX).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}
	}
	
	public void IDNumberValidator(FacesContext context, UIComponent component,
			Object value)
	{
		  pattern = Pattern.compile("[0-9]{8,255}");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("IDNumber validation failed.", 
							"Invalid IDNumber format (Number only and at least 8 numbers and max 255 numbers).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}
	}
	
	public void BathRoomValidator(FacesContext context, UIComponent component,
			Object value)
	{

		  pattern = Pattern.compile("^(0?[1-9]|[1-9][0-9])$");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("BathRoom validation failed.", 
							"Invalid BathRoom format (1 to 99 only).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}

	}
	
	public void BedRoomValidator(FacesContext context, UIComponent component,
			Object value)
	{


		  pattern = Pattern.compile("^(0?[1-9]|[1-9][0-9])$");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("BathRoom validation failed.", 
							"Invalid BedRoom format (1 to 99 only).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}

	}
	
	public void RentValidator(FacesContext context, UIComponent component,
			Object value)
	{
		if (Double.parseDouble(value.toString())!=0) {
			
		  pattern = Pattern.compile("^(((\\d{1,3})(,\\d{3})*)|(\\d+))(.\\d+)?$");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("Rent validation failed.", 
							"Invalid Rent format (Number invalid).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}
		} else {
			FacesMessage msg = 
				new FacesMessage("Rent validation failed.", 
						"Invalid Money format (Cannot be zero).");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
		}
	}
	
	public void SquareValidator(FacesContext context, UIComponent component,
			Object value)
	{
		if (Double.parseDouble(value.toString())!=0) {
			
			  pattern = Pattern.compile("^(((\\d{1,3})(,\\d{3})*)|(\\d+))(.\\d+)?$");
			  matcher = pattern.matcher(value.toString());
			  if(!matcher.matches()){
				  
					FacesMessage msg = 
						new FacesMessage("Rent validation failed.", 
								"Invalid Square format (Number invalid).");
					msg.setSeverity(FacesMessage.SEVERITY_ERROR);
					throw new ValidatorException(msg);
		 
				}
			} else {
				FacesMessage msg = 
					new FacesMessage("Rent validation failed.", 
							"Invalid Square format (Cannot be zero).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
			}
	}
	
	public void TitleValidator(FacesContext context, UIComponent component,
			Object value)
	{
		  pattern = Pattern.compile("^\\s*\\S.*$");
		  matcher = pattern.matcher(value.toString());
		  if(!matcher.matches()){
			  
				FacesMessage msg = 
					new FacesMessage("Title validation failed.", 
							"Invalid Title Format (Please input something).");
				msg.setSeverity(FacesMessage.SEVERITY_ERROR);
				throw new ValidatorException(msg);
	 
			}
		  
	}
}
